package cn.iocoder.yudao.module.fm.controller.admin.subgrade;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.fm.controller.admin.subgrade.vo.*;
import cn.iocoder.yudao.module.fm.convert.subgrade.SubgradeConvert;
import cn.iocoder.yudao.module.fm.dal.dataobject.subgrade.SubgradeDO;
import cn.iocoder.yudao.module.fm.enums.FileInfoType;
import cn.iocoder.yudao.module.fm.service.fileinfo.FileInfoService;
import cn.iocoder.yudao.module.fm.service.subgrade.SubgradeService;
import cn.iocoder.yudao.module.fm.utils.DownloadTemplateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

/**
 * @author SUN
 */
@Tag(name = "管理后台 - 路基")
@RestController
@RequestMapping("/fm/subgrade")
@Validated
public class SubgradeController {

    @Resource
    private SubgradeService subgradeService;
    @Resource
    private FileInfoService fileInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建路基")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:create')")
    public CommonResult<Long> createSubgrade(@Valid @RequestBody SubgradeCreateReqVO createReqVO) {
        return success(subgradeService.createSubgrade(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新路基")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:update')")
    public CommonResult<Boolean> updateSubgrade(@Valid @RequestBody SubgradeUpdateReqVO updateReqVO) {
        subgradeService.updateSubgrade(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除路基")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fm:subgrade:delete')")
    public CommonResult<Boolean> deleteSubgrade(@RequestParam("id") Long id) {
        subgradeService.deleteSubgrade(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得路基")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:query')")
    public CommonResult<SubgradeRespVO> getSubgrade(@RequestParam("id") Long id) {
        SubgradeDO subgrade = subgradeService.getSubgrade(id);
        SubgradeRespVO subgradeRespVO = SubgradeConvert.INSTANCE.convert(subgrade);
        subgradeRespVO.setFileList(fileInfoService.get(subgradeRespVO.getId(), FileInfoType.SUBGRADE.getValue()));
        return success(subgradeRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得路基列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:query')")
    public CommonResult<List<SubgradeRespVO>> getBridgeList(@RequestParam("ids") Collection<Long> ids) {
        List<SubgradeDO> list = subgradeService.getBridgeList(ids);
        return success(SubgradeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得路基分页")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:query')")
    public CommonResult<PageResult<SubgradeRespVO>> getBridgePage(@Valid SubgradePageReqVO pageVO) {
        PageResult<SubgradeDO> pageResult = subgradeService.getBridgePage(pageVO);
        return success(SubgradeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出路基 Excel")
    @PreAuthorize("@ss.hasPermission('fm:subgrade:export')")
    @OperateLog(type = EXPORT)
    public void exportBridgeExcel(@Valid SubgradeExportReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        List<SubgradeDO> list = subgradeService.getBridgeList(exportReqVO);
        // 导出 Excel
        List<SubgradeExcelVO> datas = SubgradeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "路基.xls", "数据", SubgradeExcelVO.class, datas);
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public ResponseEntity<byte[]> importTemplate() {
        return DownloadTemplateUtil.downloadTemplate("subgradeTemplate.xlsx", "template/subgrade-export-template-download.xlsx");
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('system:user:import')")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) {
        subgradeService.importExcel(file);
        return CommonResult.success(true);
    }


    @GetMapping("/export")
    @Operation(summary = "导出")
    @PreAuthorize("@ss.hasPermission('system:tunnel:export')")
    public ResponseEntity<byte[]> exportExcel(Long id) {
        return subgradeService.exportExcel(getSubgrade(id).getData());
    }

}
